var errorMsg = "{\"error_code\":0,\"error_msg\":\"Internet connecting failed! Please check your network configuration.\"}"

function sogouSearchAPI(observer, what, range)
{
    console.log("Start to search " + what + " ...")

    var doc = new XMLHttpRequest();

//    doc.open("GET", "http://api.go2map.com/engine/api/search/json?", true);
    doc.open("GET", "http://api.go2map.com/engine/api/search/json?what=" + what + "&range=" + range + "&contenttype=utf-8", true);
    doc.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

//    var paras = new Array();
//        paras[0] = "what";
//        paras[1] = "range";
////        paras[2] = "version";
//        paras[2] = "contenttype"

//    var values = new Array();
//        values[0] = what;
//        values[1] = range;
////        values[2] = "1.0";
//        values[2] = "UTF-8";

//    var sendstring = combine(paras, values);
//    doc.send(sendstring);
    doc.send();

    doc.onreadystatechange = function()
        {
            if(doc.readyState == XMLHttpRequest.HEADERS_RECEIVED) {
                console.log("search receiving ...");
            }
            else if(doc.readyState == XMLHttpRequest.DONE) {
                var json;
                if(doc.status == 0) {
                    json = eval('(' + errorMsg + ')');
                }
                else {
                   console.log("search received done");
                    console.log(doc.responseText);
                   json = eval('(' + doc.responseText+ ')');
                }
                observer.update(json);
            }
        }
}

function sogouLatLontoAddressAPI(observer, lat, lon)
{
    console.log("Start to convert " + lon + " , " + lat + " ...")

    var doc = new XMLHttpRequest();

//    doc.open("GET", "http://api.go2map.com/engine/api/search/json?", true);
    doc.open("GET", "http://api.go2map.com/engine/api/regeocoder/json?points=" + lon + "," + lat + "&type=1&contenttype=utf-8", true);
    doc.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    doc.send();

    doc.onreadystatechange = function()
        {
            if(doc.readyState == XMLHttpRequest.HEADERS_RECEIVED) {
                console.log("convert receiving ...");
            }
            else if(doc.readyState == XMLHttpRequest.DONE) {
                var json;
                if(doc.status == 0) {
                    json = eval('(' + errorMsg + ')');
                }
                else {
                   console.log("convert received done");
                    console.log(doc.responseText);
                   json = eval('(' + doc.responseText+ ')');
                }
                observer.update(json);
            }
        }
}

function combine(paras, values)
{
    var str = "";
    var i;
    for(i = 0; i < paras.length; i++)
    {
        if(i == 0)
        {
            str += paras[i] + "=" + urlEncode(values[i])
        }
        else
        {
            str += "&" + paras[i] + "=" + urlEncode(values[i])
        }
    }

    return str
}

function urlEncode(str)
{
    var ret="";
    var strSpecial="!\"#$%&()*+,/:;<=>?[]^`{|}~%@"; var tt="";
    for(var i=0;i<str.length;i++)
    {
        var chr = str.charAt(i);
        var c=str2asc(chr);
        tt += chr+":"+c+"n";

        if(chr==" ")
            ret+="+";
        else if(strSpecial.indexOf(chr)!=-1)
            ret+="%"+c.toString(16);
        else
            ret+=chr;
    }

    return ret;
}

function str2asc(str)
{
    return str.charCodeAt(0).toString(16);
}
